import { Button } from 'antd'
import React from 'react'
import { Link } from 'react-router-dom'
import { isAuth } from '../../helpers/auth'
import axios from 'axios'
import { API } from '../../config'

function PayButton({address, cart, totalPrice }) {
  const handlePay = () => {
    axios.post(`${API}/alipay`,{
      totalAmount: totalPrice,
      subject: '测试订单标题',
      body: '测试订单描述',
      product: cart.map(product => ({
        product: product._id,
        count: product.count
      })),
      address: address,
      userId: isAuth().user._id
    }).then(response => {
      window.location.href = response.data.result
    } )
  }
  const showButton = () => {
    return isAuth() ? 
    <Button onClick={handlePay} type='primary'>提交按钮</Button> 
    : <Button type='primary'>
        <Link to='/signin'>登录</Link>
      </Button>
  }
  return (
    <div>
      {showButton()}
    </div>
  )
}

export default PayButton
